#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 1e4 + 5;
const int K = 1e6;
int n, m;
int a[N], b[N], c[N];
bool check(int a[N], int b[N]) {
  rep(i, 1, n) if (a[i] != b[i]) return false;
  return true;
}
bool test(ll mod) {
  if (mod < b[n] + 1) return false;
  if (mod > K) return false;
  rep(i, 1, n) c[i] = a[i] % mod;
  sort(c + 1, c + n + 1);
  return check(b, c);
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    cin >> n;
    rep(i, 1, n) cin >> a[i];
    rep(i, 1, n) cin >> b[i];
    sort(a + 1, a + n + 1);
    sort(b + 1, b + n + 1);
    if (check(a, b)) {
      cout << K + 5 << endl;
      continue;
    }
    ll x = accumulate(a + 1, a + n + 1, 0ll);
    ll y = accumulate(b + 1, b + n + 1, 0ll);
    if (x <= y) {
      cout << -1 << endl;
      continue;
    }
    ll d = x - y;
    ll ans = -1;
    for (ll i = 1; i * i <= d; i++) {
      if (d % i) continue;
      if (test(i)) {
        ans = i;
        break;
      }
      ll j = d / i;
      if (j != i && test(j)) {
        ans = j;
        break;
      }
    }
    cout << ans << endl;
  }
  return 0;
}